package com.ihr360.insurance.model.dto.staff;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.entity.BasicStaffInfoEntity;
import com.ihr360.insurance.entity.vo.BenefitVo;
import com.ihr360.insurance.model.dto.benefits.CompanyBenefit;
import com.ihr360.insurance.model.dto.staff.embeddable.BenefitEntity;
import com.ihr360.shared.basicstaffinfo.dto.SyncedBasicStaffInfo;
import com.ihr360.shared.entitywithlistener.NumberIdWithTimeAndCompanyId;
import org.springframework.beans.BeanUtils;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * @author Stone.Shi
 * @description 员工社保公积金数据
 * @date 2018-02-01 15:57:16.
 */
@Entity
@Table(indexes = {@Index(name = "company_staff_index", columnList = "companyId, staff_id")})
public class StaffBenefit extends NumberIdWithTimeAndCompanyId<Long> implements Serializable {

    public static final class Field {
        public final static String id = "id";
        public final static String companyId = "companyId";

        public final static String staff = "staff";
        public final static String staff_id = "staff.id";
        public final static String staff_staffName = "staff.staffName";
        public final static String staff_idCardNo = "staff.idCardNo";
        public final static String staff_sex = "staff.sex";
        public final static String staff_departmentId = "staff.departmentId";
        public final static String staff_staffStatus = "staff.staffStatus";

        public final static String siCompanyBenefit = "siCompanyBenefit";
        public final static String siCompanyBenefit_id = "siCompanyBenefit.id";
        public final static String hfCompanyBenefit = "hfCompanyBenefit";
        public final static String hfCompanyBenefit_id = "hfCompanyBenefit.id";

        public final static String socialInsurance_base = "socialInsurance.base";
        public final static String socialInsurance_startOn = "socialInsurance.startOn";
        public final static String socialInsurance_endOn = "socialInsurance.endOn";

        public final static String houseFund_base = "houseFund.base";
        public final static String houseFund_startOn = "houseFund.startOn";
        public final static String houseFund_endOn = "houseFund.endOn";
    }

    private static final long serialVersionUID = -3074031263518121655L;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false, unique = true)
    private SyncedBasicStaffInfo staff;

    /**
     * 员工社保数据
     */
    @AttributeOverrides({
            @AttributeOverride(name = "vendorId", column = @Column(name = "si_vendor_id")),
            @AttributeOverride(name = "actualPayer", column = @Column(name = "si_actual_payer")),
            @AttributeOverride(name = "payDepartmentId", column = @Column(name = "si_pay_department_id")),
            @AttributeOverride(name = "calculatable", column = @Column(name = "si_calculatable")),
            @AttributeOverride(name = "base", column = @Column(name = "si_base")),
            @AttributeOverride(name = "startOn", column = @Column(name = "si_start_on")),
            @AttributeOverride(name = "endOn", column = @Column(name = "si_end_on")),
            @AttributeOverride(name = "divisionId", column = @Column(name = "si_division_id")),
    })

    /**
     * 社保基础数据
     * note:
     * 如果 siCompanyBenefit != null ，则 socialInsurance.divisionId必须与 siCompanyBenefit.group.divisionId一致；
     * 如果 siCompanyBenefit == null ，则 socialInsurance.divisionId值可以单独设置
     */
    @Embedded
    private BenefitEntity socialInsurance;
    /**
     * 社保账号
     */
    @Column(name = "si_account_number", length = 40)
    private String siAccountNumber;

    /**
     * 冗余字段：社保方案id
     * note:
     * 此字段仅为兼容花名册社保数据而用
     * 如果花名册中设置社保方案为旧版数据：则此值为花名册中社保缴纳类型的id值,String类型；
     * 如果花名册中设置社保方案为福利云新版数据：则此值为siCompanyBenefit的id，Long类型转换为String
     */
    @Column(name = "tmp_si_benefit_id", length = 40)
    private String tmpSiBenefitId;

    /**
     * 社保方案
     */
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    private CompanyBenefit siCompanyBenefit;

    /**
     * 员工公积金数据
     */
    @AttributeOverrides({
            @AttributeOverride(name = "vendorId", column = @Column(name = "hf_vendor_id")),
            @AttributeOverride(name = "actualPayer", column = @Column(name = "hf_actual_payer")),
            @AttributeOverride(name = "payDepartmentId", column = @Column(name = "hf_pay_department_id")),
            @AttributeOverride(name = "calculatable", column = @Column(name = "hf_calculatable")),
            @AttributeOverride(name = "base", column = @Column(name = "hf_base")),
            @AttributeOverride(name = "startOn", column = @Column(name = "hf_start_on")),
            @AttributeOverride(name = "endOn", column = @Column(name = "hf_end_on")),
            @AttributeOverride(name = "divisionId", column = @Column(name = "hf_division_id")),
    })

    /**
     * 公积金基础数据
     * note:
     * 如果 hfCompanyBenefit != null ，则 houseFund.divisionId必须与 hfCompanyBenefit.group.divisionId一致；
     * 如果 hfCompanyBenefit == null ，则 houseFund.divisionId值可以单独设置
     */
    @Embedded
    private BenefitEntity houseFund;

    /**
     * 公积金账号
     */
    @Column(name = "hf_account_number", length = 40)
    private String hfAccountNumber;

    /**
     * 补充公积金账号
     */
    @Column(name = "supplementary_hf_account_number", length = 40)
    private String supplementaryHfAccountNumber;

    /**
     * 公积金方案
     */
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    private CompanyBenefit hfCompanyBenefit;

    public SyncedBasicStaffInfo getStaff() {
        return staff;
    }

    public void setStaff(SyncedBasicStaffInfo staff) {
        this.staff = staff;
    }

    public BenefitEntity getSocialInsurance() {
        return socialInsurance;
    }

    public void setSocialInsurance(BenefitEntity socialInsurance) {
        this.socialInsurance = socialInsurance;
    }

    public CompanyBenefit getSiCompanyBenefit() {
        return siCompanyBenefit;
    }

    public void setSiCompanyBenefit(CompanyBenefit siCompanyBenefit) {
        this.siCompanyBenefit = siCompanyBenefit;
    }

    public String getTmpSiBenefitId() {
        return tmpSiBenefitId;
    }

    public void setTmpSiBenefitId(String tmpSiBenefitId) {
        this.tmpSiBenefitId = tmpSiBenefitId;
    }

    public BenefitEntity getHouseFund() {
        return houseFund;
    }

    public void setHouseFund(BenefitEntity houseFund) {
        this.houseFund = houseFund;
    }

    public CompanyBenefit getHfCompanyBenefit() {
        return hfCompanyBenefit;
    }

    public void setHfCompanyBenefit(CompanyBenefit hfCompanyBenefit) {
        this.hfCompanyBenefit = hfCompanyBenefit;
    }

    public String getSiAccountNumber() {
        return siAccountNumber;
    }

    public void setSiAccountNumber(String siAccountNumber) {
        this.siAccountNumber = siAccountNumber;
    }

    public String getHfAccountNumber() {
        return hfAccountNumber;
    }

    public void setHfAccountNumber(String hfAccountNumber) {
        this.hfAccountNumber = hfAccountNumber;
    }

    public String getSupplementaryHfAccountNumber() {
        return supplementaryHfAccountNumber;
    }

    public void setSupplementaryHfAccountNumber(String supplementaryHfAccountNumber) {
        this.supplementaryHfAccountNumber = supplementaryHfAccountNumber;
    }

    /************************************员工基本数据数据*************************************************/

    @JsonIgnore
    public BasicStaffInfoEntity getStaffInfoVo() {
        if (staff == null) {
            return null;
        }
        BasicStaffInfoEntity result = new BasicStaffInfoEntity();
        BeanUtils.copyProperties(staff, result);
        return result;
    }

    /************************************员工社保数据*************************************************/

    @JsonIgnore
    public BenefitVo getSocialInsuranceVo() {
        return getBenefitVo(socialInsurance, siCompanyBenefit);
    }

    /************************************员工公积金数据*************************************************/
    @JsonIgnore
    public BenefitVo getHouseFundVo() {
        return getBenefitVo(houseFund, hfCompanyBenefit);
    }

    private BenefitVo getBenefitVo(BenefitEntity benefitEntity, CompanyBenefit companyBenefit) {
        if (benefitEntity == null && companyBenefit == null) {
            return null;
        }
        BenefitVo result = new BenefitVo();
        if (benefitEntity != null) {
            BeanUtils.copyProperties(benefitEntity, result);
        }
        if (companyBenefit != null) {
            result.setOriginalCompanyBenefitId(companyBenefit.getId());
            result.setCompanyBenefitId(companyBenefit.getId());
            result.setCompanyBenefitName(companyBenefit.getName());
        }
        return result;
    }
}
